package com.supertimetable.db;

import android.content.Context;
import android.database.Cursor;

import com.supertimetable.bean.Teacher;
import com.supertimetable.constant.Constant;

import java.util.ArrayList;



public class DBTeacherUtil {
    //操作与教师类相关的操作。

    //查询教师
    public static ArrayList<Teacher> queryTeachers(Context context){
        ArrayList<Teacher> teachers = new ArrayList<>();
        MyDBHelper helper = new MyDBHelper(context);
        try {
            String sql = "select * from "+ Constant.TEACHER_TABLE;
            Cursor c=helper.getReadableDatabase().rawQuery(sql,null);
            while (c.moveToNext()) {
                System.out.print("id ："+c.getString(c.getColumnIndex("tId")));
                System.out.print("姓名："+c.getString(c.getColumnIndex("tName")) + "");
                System.out.println("性别："+c.getString(c.getColumnIndex("tSex")));
                Teacher t = new Teacher();
                t.settId(c.getString(c.getColumnIndex("tId")));
                t.settName(c.getString(c.getColumnIndex("tName")));
                teachers.add(t);
            }
        }catch (Exception e){
            System.out.println("查询异常");
        }
        return teachers;
    }

//    public static void queryTeacher(Context context,)

    //删除教师表数据
    public static void deleteTeacher(Context context){
        try {
            MyDBHelper helper = new MyDBHelper(context);
            String sql = "delete from "+ Constant.TEACHER_TABLE;
            helper.getWritableDatabase().execSQL(sql);

        }catch (Exception e){
            System.out.println("删除表数据异常");
        }

    }
    //删除教师表
    public static void dropTeacher(Context context){
        try {
            MyDBHelper helper = new MyDBHelper(context);
            String sql = "drop table "+Constant.TEACHER_TABLE;
            helper.getWritableDatabase().execSQL(sql);

        }catch (Exception e){
            System.out.println("删除表异常");
        }

    }

    //将ArrayList<Teacher> 所有的老师添加到数据库中。
    public static void insertTeacher(Context context, ArrayList<Teacher> list){
        System.out.println("insert -----------"+list.size());
        MyDBHelper helper = new MyDBHelper(context);
        for(int i = 0; i < 100; i++){
            try {
                String id = list.get(i).gettId();
                String name = list.get(i).gettName();
                String sql = "insert into "+Constant.TEACHER_TABLE+" (tId,tName) values('"+id+"','"+name+"')";
                helper.getWritableDatabase().execSQL(sql);
            }catch (Exception e){
                System.out.println("插入异常");
            }

        }
    }

}
